/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sk.stancel.peter.sudoku.types;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import android.content.Context;
import android.util.Log;

/**
 *
 * @author Peter Stancel
 */
public class SudokuTypes implements Type {

	private Context context;
	
	public SudokuTypes(Context context) {
		this.context = context;
	}
	
    /**
     * Selects one sudoku from the database.
     * @return field of hard sudoku
     */
    public int[] selectSudokuFromDB(SudokuType type) {
        int[] ints = new int[81];
//        InputStream in = null;
//        StringBuilder s = new StringBuilder();
        try {
        	InputStream in = null;
        	Random random = new Random();
        	int riadok = 0;
        	switch (type) {
			case HARD:
				in = context.getAssets().open("sudoku17.rtf");
				riadok = random.nextInt(49147);
				break;
			case DIAGONALS:
				in = context.getAssets().open("diagonals.rtf");
				riadok = random.nextInt(290);
				break;
			case POINTS:
				in = context.getAssets().open("points.rtf");
				riadok = random.nextInt(150);
				break;
			case SQUARES:
				in = context.getAssets().open("squares.rtf");
				riadok = random.nextInt(10);
				break;
			}
        	
        	BufferedReader br = new BufferedReader(new InputStreamReader(in));
        	String line;
        	
        	int currentLine = 0;
        	while((line = br.readLine()) != null){
        		if(currentLine == riadok){
        			line.trim();
        			for (int i = 0; i < 81; i++) {
        				ints[i] = Character.getNumericValue(line.charAt(i));
        			}
        			return ints;
              }
        		currentLine++;
        	}
        	br.close();
        	in.close();
        	

//            in = context.getAssets().open("sudoku17.rtf");
//            BufferedInputStream bis = new BufferedInputStream(in);
//
//            Random random = new Random();
//            int cislo;
//            int dfg = 0;
//            cislo = random.nextInt(49147);
//            while (bis.available() > 0) {
//                for (int i = 0; i < 83; i++) {
//                    s.append((char) bis.read());
//                }
//                if(dfg == cislo){
//                    s.delete(81, 83);
//                    for (int i = 0; i < 81; i++) {
//                        ints[i] = Character.getNumericValue(s.charAt(i));
//                    }
//                    s.delete(0, 81);
//                    return ints;
//                } else {
//                    s.delete(0, 83);
//                }
//                dfg++;
//            }
//            bis.close();
//            in.close();
        } catch (IOException e) {
            Log.e("SUDOKU", "Exception occured during connecting to file: " + e.getMessage());
            return null;
        }
        return ints;
    }
}
